<template>
  <el-dialog
    class="mindFileDialog"
    :title="$t('相关附件')"
    :visible.sync="dialogVisible"
    width="700"
    append-to-body
    destroy-on-close
  >
    <div class="">
      <el-form ref="form" :model="form" :rules="rules" class="dataForm">
        <FileTable :list.sync="form.fileList" data-type="mind" has-permi="mind" :btn-show="!otherBtnHide" />
      </el-form>

    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">{{ $t('dialog.cancel') }}</el-button>
      <el-button type="primary" @click="confirm">{{ $t('dialog.confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
    name: 'NodeFile',
    data() {
      return {
        dialogVisible: false,
        activeNodes: [],
        loading: false,
        iconList: [],
        form: {
            fileList: []
        },
        rules: {},
        otherBtnHide: false
      }
    },
    created() {
      this.$bus.$on('node_active', (...args) => {
        this.activeNodes = args[1]
        if (this.activeNodes.length > 0) {
          const firstNode = this.activeNodes[0]
          this.iconList = firstNode.getData('icon') || []
          this.form.fileList = firstNode.getData('fileList') || []
          // console.log('监听节点', this.activeNodes, this.form.fileList)
        } else {
          this.iconList = []
          this.form.fileList = []
        }
      })
      this.$bus.$on('showNodeFile', () => {
        this.$bus.$emit('startTextEdit')
        this.dialogVisible = true
      })
    },
    methods: {
      cancel() {
        this.dialogVisible = false
        this.$bus.$emit('endTextEdit')
      },
      confirm() {
        var type = 'other2'
        var name = 'file'
        const key = type + '_' + name
        const index = this.iconList.findIndex(item => {
          return item === key
        })
        if (this.form.fileList.length > 0) {
          if (index === -1) {
            // 增加icon
            this.iconList.push(key)
          }
        } else {
          // 删除icon
          if (index !== -1) {
            this.iconList.splice(index, 1)
          }
        }
        this.activeNodes.forEach(node => {
          node.setIcon([...this.iconList])
          node.nodeData.data.fileList = this.form.fileList
        })
        this.cancel()
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>

